import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextArea;

public class DataStructures extends JFrame {

	private JTextArea textAreaIn = null;
	private JTextArea textAreaOut = null;

	public static void main(String args[]) {
		try {
			DataStructures frame = new DataStructures();
			frame.setVisible(true);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public DataStructures() {
		super();
		setTitle("Sort");
		getContentPane().setLayout(null);
		setBounds(100, 100, 407, 359);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

		textAreaIn = new JTextArea();
		textAreaIn.setBounds(33, 41, 333, 98);
		getContentPane().add(textAreaIn);

		final JLabel label = new JLabel();
		label.setText("Input array，split by comma:");
		label.setBounds(33, 10, 233, 15);
		getContentPane().add(label);

		final JButton button = new JButton();
		button.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				showResult();
			}
		});
		button.setText("Sort");
		button.setBounds(145, 152, 99, 23);
		getContentPane().add(button);

		textAreaOut = new JTextArea();
		textAreaOut.setBounds(33, 203, 333, 98);
		getContentPane().add(textAreaOut);

		final JLabel label_1 = new JLabel();
		label_1.setText("Sort result:");
		label_1.setBounds(33, 174, 143, 15);
		getContentPane().add(label_1);
	}

	private void showResult() {
		String inputStr = textAreaIn.getText();
		String[] input = inputStr.split(",");
		int[] inputArray = new int[input.length];
		for (int i = 0; i < input.length; i++) {
			inputArray[i] = Integer.parseInt(input[i]);
		}
		int[] result = this.getSortResult(inputArray);
		String resultStr = result[0] + "";
		for (int i = 1; i < result.length; i++) {
			resultStr = resultStr + "," + result[i];
		}
		textAreaOut.setText(resultStr);
	}

	private int[] getSortResult(int[] sortArray) {
		for (int i = 0; i < sortArray.length; i++) {
			for (int j = i + 1; j < sortArray.length; j++) {
				if (sortArray[i] < sortArray[j]) {
					int temp = sortArray[i];
					sortArray[i] = sortArray[j];
					sortArray[j] = temp;
				}
			}
		}
		return sortArray;
	}

}
